package lib

import (
	"math"
)

func init() {
	Probs = append(Probs, Problem{
		Num:         162,
		Discription: "找一个相邻元素不相等的数组峰值",
		Level:       2,
		Labels: map[string]int{
			"二分查找": 1,
			"贪心": 1,
		},
	})
}

func FindPeakElement(nums []int) int {
	left := 0
	right := len(nums) - 1
	for left <= right {
		//注意左右边界
		mid := left + (right-left)/2
		var pre, next int
		if mid-1 < 0 {
			pre = math.MinInt64
		} else {
			pre = nums[mid-1]
		}
		if mid+1 >= len(nums) {
			next = math.MinInt64
		} else {
			next = nums[mid+1]
		}

        //递增方向必有解
		//举例子mid<next:next+1<next, next为解， next+1>next，继续往右，到了边界必有next+1<next
		if pre < nums[mid] && nums[mid] > next {
			return mid
		}

		if pre >= nums[mid] && nums[mid] <= next {
			right = mid - 1
			continue
		}

		left = mid + 1
	}

	return -1
}
